import { WaveOptions, StripOptions } from '.';

export const drawWave = (
  ctx: CanvasRenderingContext2D,
  data: Uint8Array,
  config: Required<WaveOptions> & {
    height: number;
    width: number;
  }
) => {
  const { color, lineWidth, height, width } = config;
  ctx.clearRect(0, 0, width, height);
  ctx.lineWidth = lineWidth;
  ctx.strokeStyle = color;
  ctx.beginPath();
  let x = 0;
  const sliceWidth = width / data.length;
  data.forEach((item, i) => {
    const v = item / 128;
    const y = (v * height) / 2;
    if (i === 0) {
      ctx.moveTo(x, y);
    } else {
      ctx.lineTo(x, y);
    }
    x += sliceWidth;
  });
  ctx.lineTo(width, height / 2);
  ctx.stroke();
};

export const drawStrip = (
  ctx: CanvasRenderingContext2D,
  data: Uint8Array,
  config: Required<StripOptions> & {
    height: number;
    width: number;
  }
) => {
  const { color, lineWidth, height, width, gap, heightDivisor } = config;
  ctx.clearRect(0, 0, width, height);
  ctx.strokeStyle = color;
  ctx.lineWidth = lineWidth;
  const start = width / 2 - (data.length * lineWidth) / 2;
  data.forEach((item, i) => {
    const itemData = item - 128;
    ctx.beginPath();
    ctx.moveTo(start + i * gap, height / 2 - itemData / (height / heightDivisor));
    ctx.lineTo(start + i * gap, height / 2 + itemData / (height / heightDivisor));
    ctx.stroke();
  });
};
